﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameServer.Common
{
    public class TimedParameteredDelegateList
    {
        private List<ParameteredDelegateData> delegateList;
        private List<int> timers;

        public TimedParameteredDelegateList()
        {
            delegateList = new List<ParameteredDelegateData>(5);
            timers = new List<int>(5);
        }


        public void Add(ParameteredDelegateData deleg, int delay)
        {
            delegateList.Add(deleg);
            timers.Add(delay);
        }

        public void Tick(int numTicks)
        {
            List<int> indexesMarkedForRemoval;
            for (int i = 0; i < numTicks; i++)
            {
                indexesMarkedForRemoval = new List<int>();
                for (int j = 0; j < timers.Count; j++)
                {
                    timers[j]--;
                    if (timers[j] == 0)
                    {
                        indexesMarkedForRemoval.Add(j);
                        delegateList[j].Invoke();
                    }
                }
                for (int j = indexesMarkedForRemoval.Count - 1; j >= 0; j--)
                {
                    timers.RemoveAt(j);
                    delegateList.RemoveAt(j);
                }
            }

        }
    }
}
